home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / progut~1 / gperf.zoo / src / read-line.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-25  |  2.0 KB  |  85 lines

  1. /* Correctly reads an arbitrarily size string.
  2.  
  3.    Copyright (C) 1989 Free Software Foundation, Inc.
  4.    written by Douglas C. Schmidt (schmidt@ics.uci.edu)
  5.  
  6. This file is part of GNU GPERF.
  7.  
  8. GNU GPERF is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 1, or (at your option)
  11. any later version.
  12.  
  13. GNU GPERF is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with GNU GPERF; see the file COPYING.  If not, write to
  20. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22. #include <std.h>
  23. #include "std-err.h"
  24. #include "read-line.h"
  25.  
  26. /* Recursively fills up the buffer. */
  27.  
  28. char *
  29. Read_Line::readln_aux (int chunks)
  30. {
  31.   T (Trace t ("Read_Line::readln_aux");)
  32.   char buf[CHUNK_SIZE];
  33.   char *bufptr = buf;
  34.   char *ptr;
  35.   int c;
  36.  
  37.   while ((c = getc (fp)) != EOF && c != '\n') /* fill the current buffer */
  38.     {
  39.       *bufptr++ = c;
  40.       if (bufptr - buf >= CHUNK_SIZE) /* prepend remainder to ptr buffer */
  41.         {
  42.           if (ptr = readln_aux (chunks + 1))
  43.  
  44.             for (; bufptr != buf; *--ptr = *--bufptr);
  45.  
  46.           return ptr;
  47.         }
  48.     }
  49.   if (c == EOF && bufptr == buf)
  50.     return 0;
  51.  
  52.   c   = chunks * CHUNK_SIZE + bufptr - buf + 1;
  53.   ptr = new char[c];
  54.  
  55.   for (*(ptr += (c - 1)) = '\0'; bufptr != buf; *--ptr = *--bufptr)
  56.     ;
  57.  
  58.   return ptr;
  59. }
  60.  
  61. #ifndef __OPTIMIZE__
  62.  
  63. /* Returns the ``next'' line, ignoring comments beginning with '#'. */
  64.  
  65. char *
  66. Read_Line::get_line (void) 
  67. {
  68.   T (Trace t ("Read_Line::get_line");)
  69.   int c;
  70.  
  71.   if ((c = getc (fp)) == '#')
  72.     {
  73.       while ((c = getc (fp)) != '\n' && c != EOF)
  74.         ;
  75.  
  76.       return c != EOF ? get_line () : 0;
  77.     }
  78.   else
  79.     {
  80.       ungetc (c, stdin);
  81.       return readln_aux (0);
  82.     }
  83. }
  84. #endif
  85.